package com.magictower.primary.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.magictower.primary.R;
import com.magictower.primary.adapter.BlackCallBaseAdapter;
import com.magictower.primary.bean.BlackNumberInfo;
import com.magictower.primary.db.dao.BlackNumberDao;

import java.util.List;

public class CallSafeActivity extends AppCompatActivity {
    private LinearLayout pb;
    private ListView listView;
    private EditText input;
    private TextView show;

    private int pageSize = 10; // 每页显示多少条数据
    private int currentPage = 0; // 当前页
    private int totalPage;
    private List<BlackNumberInfo> list;
    private BlackNumberDao dao;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_call_safe);
        initUI();
        initData();
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            pb.setVisibility(View.INVISIBLE);
            show.setText((currentPage + 1 )+ "/" + (totalPage + 1));
            listView.setAdapter(new BlackCallBaseAdapter(CallSafeActivity.this, list));
        }
    };

    private void initUI() {
        pb = (LinearLayout) findViewById(R.id.ll_pb);
        pb.setVisibility(View.VISIBLE);
        listView = (ListView) findViewById(R.id.list_view);
        input = (EditText) findViewById(R.id.et_page_number);
        show = (TextView) findViewById(R.id.tv_page_number);
    }

    private void initData() {
        new Thread() {
            @Override
            public void run() {
                dao = new BlackNumberDao(CallSafeActivity.this);
                totalPage = dao.getTotalNumber()/pageSize;
                System.out.println("总的页码=" + (totalPage + 1) + "；当前页码=" + (currentPage + 1));
                list = dao.findPar(currentPage, pageSize);
                handler.sendEmptyMessage(0);
            }
        }.start();
    }

    public void prePage(View view) {
        if (currentPage != 0) {
            currentPage--;
            initData();
        } else {
            Toast.makeText(CallSafeActivity.this, "当前是第一页", Toast.LENGTH_SHORT).show();
        }
    }

    public void nextPage(View view) {
        if (currentPage < totalPage) {
            currentPage++;
            initData();
        } else {
            Toast.makeText(CallSafeActivity.this, "当前是第后页", Toast.LENGTH_SHORT).show();
        }
    }

    public void jump(View view) {
        String inputStr = input.getText().toString().trim();
        if (TextUtils.isDigitsOnly(inputStr) && !TextUtils.isEmpty(inputStr)) {
            int inputPage = Integer.valueOf(inputStr);
            if (inputPage <= totalPage+1 && inputPage > 0) {
                currentPage = inputPage-1;
                //输入合法
                initData();
            } else {
                Toast.makeText(CallSafeActivity.this, "输入页码无数据", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(CallSafeActivity.this, "输入页码不合法", Toast.LENGTH_SHORT).show();
        }
    }

    public void addBlackCall(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("新增黑名单");
        builder.setIcon(R.drawable.ic_delete_btn_focused);
        View v = View.inflate(this, R.layout.dialog_add_black_call, null);
        builder.setView(v);
        final EditText addBlackCall = (EditText) v.findViewById(R.id.et_add_black_call);
        final CheckBox cbCall = (CheckBox) v.findViewById(R.id.cb_forbid_call);
        final CheckBox cbMsg = (CheckBox) v.findViewById(R.id.cb_forbid_msg);
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String phone = addBlackCall.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    Toast.makeText(CallSafeActivity.this, "请添加号码", Toast.LENGTH_SHORT).show();
                    return;
                }
                String mode;
                if (cbCall.isChecked() && cbMsg.isChecked()) {
                    mode = String.valueOf(3);
                } else if (cbCall.isChecked()) {
                    mode = String.valueOf(1);
                } else if (cbMsg.isChecked()) {
                    mode = String.valueOf(2);
                } else {
                    Toast.makeText(CallSafeActivity.this, "请选择拦截方式", Toast.LENGTH_SHORT).show();
                    return;
                }
                boolean res = dao.save(phone, mode);
                if (res) {
                    Toast.makeText(CallSafeActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                    initData();
                } else {
                    Toast.makeText(CallSafeActivity.this, "失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
        builder.show();
    }


}
